package com.joker.repository;

import java.sql.*;

public class Rootupdatezw {
    public boolean update(String s1,String s2){
        String username=s1;
        String zwname=s2;
        ResultSet resultSet=null;

        int i=0;
        Connection connection= null;
        Boolean b1=true;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url="jdbc:mysql://localhost:3306/huizhan?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC";
            String root="root";
            String password="123456";


            try {
                connection = DriverManager.getConnection(url,root,password);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

            String sql="select * from user_zhanwei where username=? and zwname=?";
            try {
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1,username);
                preparedStatement.setString(2,zwname);

                resultSet=preparedStatement.executeQuery();
//                System.out.println("这是结果"+resultSet);
                b1=resultSet.next();
                System.out.println("输出布尔结果"+b1);
                if(b1)
                {
                    sql="update user_zhanwei set zhuangtai=? where username=? and zwname=?";
                    preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setString(1,"1");
                    preparedStatement.setString(2,username);
                    preparedStatement.setString(3,zwname);
                    i=preparedStatement.executeUpdate();
                    System.out.println("输出插入后的数据返回值"+i);

//                    修改主表
//                    这里查找展馆信息用的是展馆名，所以后续创建展馆时展馆名不能重复
                    sql="update zhanwei set zhuangtai=? where zwname=?";
                    preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setString(1,"1");
                    preparedStatement.setString(2,zwname);
                    i=preparedStatement.executeUpdate();
                }
//                System.out.println(resultSet);
            }
            catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }


        if(b1){
            return true;
        }
        else {
            return false;
        }

    }
}
